package com.example.demo.practice.monitoring;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.SimpleAdapter;

import com.example.demo.practice.R;
import com.example.demo.practice.innerclass.IBaseActivity;
import com.example.demo.practice.tool.ServerHttp;
import com.example.demo.practice.ui.MapAndList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class SmokeList extends IBaseActivity {
    private MapAndList mapAndList;


    private List<Map<String, Object>> groupArray;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_video_list);
        //更改标题
        setTitle("烟雾监控");
        mapAndList = (MapAndList) findViewById(R.id.data);
        mapAndList.loadUrl("html/monitor/Smoke.html");
        serverHttp = new ServerHttp(new UpdateUI());
        serverHttp.setUrl("smoke");

    }

    class UpdateUI extends Handler {
        @Override
        public void handleMessage(Message msg) {
            JSONArray jsonArray=serverHttp.getData();
            groupArray = new ArrayList<Map<String, Object>>();
            for (int i = 0; i < jsonArray.length(); i++) {
                Map<String, Object> map = new HashMap<String, Object>();
                JSONObject json = null;
                try {
                    json = (JSONObject) jsonArray.get(i);
                    map.put("code", "SXC" + i);
                    map.put("company", json.getString("company"));
                    map.put("address", json.getString("address"));
                    map.put("geom", json.getString("geom"));
                    map.put("smoke", json.getString("smoke") + "%");
                    groupArray.add(map);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
            jsonArray = null;
            serverHttp.clear();
            //适配列表
            SimpleAdapter simpleAdapter = new SimpleAdapter(SmokeList.this,
                    groupArray,
                    R.layout.list_item_temp,
                    new String[]{"code", "company", "address", "smoke"},
                    new int[]{R.id.code, R.id.company, R.id.address, R.id.temp});
            mapAndList.setListAdapter(simpleAdapter);
            mapAndList.setItemLongClickListener(new LongClick());
            mapAndList.setItemClickListener(new SingleClick());
            super.handleMessage(msg);
        }
    }


    public class LongClick implements AdapterView.OnItemLongClickListener {

        @Override
        public boolean onItemLongClick(AdapterView<?> adapterView, View view, int i, long l) {
            Intent intent = null;
            try {
                intent = new Intent(SmokeList.this, Class.forName("com.example.demo.practice.dashboardview.SmokeView"));
                Map<String, Object> map = groupArray.get(i);
                intent.putExtra("code", (String) map.get("code"));
                intent.putExtra("company", (String) map.get("company"));
                intent.putExtra("address", (String) map.get("address"));
                startActivity(intent);
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
            return false;
        }
    }

    public class SingleClick implements AdapterView.OnItemClickListener {

        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
            Map<String, Object> map = groupArray.get(i);
            String geom = String.valueOf(map.get("geom"));
            int s1 = geom.indexOf("[");
            int s2 = geom.indexOf("]");
            String coordinates = geom.substring(s1 + 1, s2);
            String[] latLng = coordinates.split(",");
            double lng = Double.parseDouble(latLng[0]);
            double lat = Double.parseDouble(latLng[1]);
            mapAndList.setMapCenter(lat, lng);
        }
    }
}
